import React, {Component} from 'react';
import 'antd/dist/antd.css'
import '@/common.css'
import '@/static/iconfont/iconfont.css'
import Header from '@/components/Header'
import NavLeft from '@/components/NavLeft/index'
import MainTab from '@/components/MainTab'
import MainBody from '@/components/MainBody'
import {Row, Col} from 'antd'
import {connect} from 'react-redux'
import {Redirect} from 'react-router-dom'
import {store} from '@/store'
import {actionFunc as loginActionFunc} from '@/components/Login/store'

class Admin extends Component {
    render() {
        const content = (
            <div>
                <Row><Header/></Row>
                <Row>
                    <Col span={4}>
                        <NavLeft/>
                    </Col>
                    <Col span={20}>
                        <Row><MainTab></MainTab></Row>

                        <Row>
                            <MainBody>
                                {this.props.children}
                            </MainBody>
                        </Row>
                    </Col>
                </Row>
            </div>
        )
        const cacheLoginStatus = localStorage.getItem('loginStatus');
        if (this.props.loginStatus === true ) { //登录状态为true, 返回主页
            return content
        } else {
            if ( cacheLoginStatus === 'true') { //登录状态为false,缓存登录状态为true,说明是用户在刷新页面，返回首页
                store.dispatch(loginActionFunc.changeLoginStatus(true))
                return content
            } else { // 登录状态为false， 返回登录页面
                return <Redirect to='/login'/>
            }
        }
    }
}

const mapState = (state) => ({
    loginStatus: state.getIn(['login', 'loginStatus'])
})
export default connect(mapState, null)(Admin);
